package command_pattern.quash;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 应用程序类，负责管理编辑器、按钮、快捷键和历史命令。
 * @author 申书航
 * @version 1.0
 */
@Data
public class Application {

    private Editor editor;

    private List<Button> buttons;

    private List<ShortCut> shortCuts;

    private String clipboard;

    private HistoryCommand history;


    public Application() {
        // 构造方法，初始化按钮、快捷键和历史命令
        buttons = new ArrayList<Button>();
        shortCuts = new ArrayList<>();
        history = new HistoryCommand();
        init();
    }


    public void init() {
        // 初始化编辑器
        editor = new Editor();
        editor.replaceText("初始文本");
        // 初始化按钮
        buttons.add(new Button(new CopyCommand(this, editor)));
        buttons.add(new Button(new PasteCommand(this, editor)));
        buttons.add(new Button(new CutCommand(this, editor)));
        // 初始化快捷键
        shortCuts.add(new ShortCut(new CopyCommand(this, editor)));
        shortCuts.add(new ShortCut(new PasteCommand(this, editor)));
        shortCuts.add(new ShortCut(new CutCommand(this, editor)));
    }


    public void doCommand() {
        // 执行命令，包括快捷键和按钮的操作
        ShortCut shortCut = shortCuts.get(2);
        shortCut.key();
        history.push(shortCut.getCommand());

        Button button = buttons.get(1);
        button.click();
        history.push(button.getCommand());
    }

    public void undo() {
        // 撤销最后一步操作
        Command pop = history.pop();
        if (pop != null) {
            System.out.println("撤销前文本内容：" + editor.getText());
            pop.undo();
            System.out.println("撤销后文本内容：" + editor.getText());
        }
    }


    public static void main(String[] args) {
        // 应用程序入口
        Application app = new Application();
        app.doCommand();
        app.undo();
        app.undo();
        app.undo();
    }
}
